import {API} from "@/api/MyAxiosConfig";
import type {
    EnterpriseRegister,
    LoginDTO,
    StudentInterface,
    StudentRegisterDTO,
    UpdateEnterpriseInfoInterface,
    UpdatePasswordInterface
} from "@/model/type";

/**
 * 登录接口
 * @param user 包含登录信息的对象，类型为LoginDTO
 * @returns 返回登录成功后的响应数据
 */
export async function loginAPI(user: LoginDTO) {
    try {
        let path = "";
        if (user.userType === "student") {
            path = "/students";
        } else if (user.userType === "enterprise") {
            path = "/enterprise";
        }
        const response = await API({
            url: path + "/login",
            method: "POST",
            data: user
        })
        return response.data;
    } catch (error) {
        console.error('axios error:', error.message);
    }
}

/**
 * 学生注册接口
 * @param user 包含学生注册信息的对象，类型为StudentRegisterDTO
 * @returns 返回注册成功的响应数据
 */
export async function studentRegisterAPI(user: StudentRegisterDTO) {
    try {
        const response = await API({
            url: "/students/register",
            method: "POST",
            data: user
        })
        return response.data;
    } catch (error) {
        console.error('axios error:', error.message);
    }
}

/**
 * 企业注册接口
 * @param user 包含企业注册信息的对象，类型为EnterpriseRegister
 * @returns 返回注册成功的响应数据
 */
export async function enterpriseRegisterAPI(user: EnterpriseRegister) {
    try {
        const response = await API({
            url: "/enterprise/register",
            method: "POST",
            data: user
        })
        return response.data;
    } catch (error) {
        console.error('axios error:', error.message);
    }
}

/**
 * 通过token获取学生信息接口
 * @param tokenStr token字符串
 * @returns 返回学生信息的响应数据
 */
export async function getStudentInfoByTokenAPI(tokenStr: string) {
    try {
        const response = await API({
            url: "/students/info",
            method: "GET",
            headers: {
                Authorization: `Bearer ${tokenStr}`
            }
        })
        return response.data;
    } catch (error) {
        console.error('axios error:', error.message);
    }
}

/**
 * 通过id和token获取学生信息接口
 * @param id 学生id
 * @param tokenStr token字符串
 * @returns 返回学生信息的响应数据
 */
export async function getStudentInfoByIdAPI(id: string, tokenStr: string) {
    try {
        const response = await API({
            url: "/students/info/" + id,
            method: "GET",
            headers: {
                Authorization: `Bearer ${tokenStr}`
            }
        })
        return response.data;
    } catch (error) {
        console.error('axios error:', error.message);
    }
}

/**
 * 更新学生信息接口
 * @param user 包含更新后学生信息的对象，类型为StudentInterface
 * @param tokenStr token字符串
 * @returns 返回更新成功的响应数据
 */
export async function updateStudentInfoAPI(user: StudentInterface, tokenStr: string) {
    try {
        const response = await API({
            url: "/students/update",
            method: "POST",
            data: user,
            headers: {
                Authorization: `Bearer ${tokenStr}`
            }
        })
        return response.data;
    } catch (error) {
        console.error('axios error:', error.message);
    }
}

/**
 * 更新学生密码接口
 * @param form 包含密码更新信息的对象，类型为UpdatePasswordInterface
 * @param tokenStr token字符串
 * @returns 返回更新成功的响应数据
 */
export async function updateStudentPasswordAPI(form: UpdatePasswordInterface, tokenStr: string) {
    try {
        const response = await API({
            url: "/students/update/password",
            method: "POST",
            data: form,
            headers: {
                Authorization: `Bearer ${tokenStr}`
            }
        })
        return response.data;
    } catch (error) {
        console.error('axios error:', error.message);
    }
}

/**
 * 通过token获取企业信息接口
 * @param tokenStr token字符串
 * @returns 返回企业信息的响应数据
 */
export async function getEnterpriseInfoByTokenAPI(tokenStr: string) {
    try {
        const response = await API({
            url: "/enterprise/info",
            method: "GET",
            headers: {
                Authorization: `Bearer ${tokenStr}`
            }
        })
        return response.data;
    } catch (error) {
        console.error('axios error:', error.message);
    }
}

/**
 * 更新企业信息接口
 * @param user 包含更新后企业信息的对象，类型为UpdateEnterpriseInfoInterface
 * @param tokenStr token字符串
 * @returns 返回更新成功的响应数据
 */
export async function updateEnterpriseInfoAPI(user: UpdateEnterpriseInfoInterface, tokenStr: string) {
    try {
        const response = await API({
            url: "/enterprise/update",
            method: "POST",
            data: user,
            headers: {
                Authorization: `Bearer ${tokenStr}`
            }
        })
        return response.data;
    } catch (error) {
        console.error('axios error:', error.message);
    }
}

/**
 * 更新企业密码接口
 * @param form 包含密码更新信息的对象，类型为UpdatePasswordInterface
 * @param tokenStr token字符串
 * @returns 返回更新成功的响应数据
 */
export async function updateEnterprisePasswordAPI(form: UpdatePasswordInterface, tokenStr: string) {
    try {
        const response = await API({
            url: "/enterprise/update/password",
            method: "POST",
            data: form,
            headers: {
                Authorization: `Bearer ${tokenStr}`
            }
        })
        return response.data;
    } catch (error) {
        console.error('axios error:', error.message);
    }
}
